//
//  AdvisoryTableView.m
//  YZZX
//
//  Created by Easaa on 15/6/10.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "AdvisoryTableView.h"

#import "AdvisoryModel.h"

#import "AdvisoryForCell.h"

#import "QuestDetailsVC.h"

@implementation AdvisoryTableView
{
    HXHttpRequest   *_httpRequest;
    NSMutableArray   *DataManage;
}

- (id)initWithActionFantionFram:(CGRect)frame Activitiesid:(NSString *)massge_id
{
    self = [super initWithFrame:frame];
    if (self)
    {
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
      
        self.id_massge = [numberFormatter stringFromNumber:massge_id];//[NSString stringWithFormat:@"%@",[NSNumber nu]];
        self.backgroundColor =[UIColor clearColor];
        DataManage  = [[NSMutableArray alloc]init];
        self.dataSource =self;
        self.delegate =self;
        UIView *v0 = [[UIView alloc] initWithFrame:CGRectZero];
        [self setTableFooterView:v0];
        
         [self creatTitleHeadView];
        
        __weak typeof(self) weakSelf = self;
        // 添加传统的下拉刷新,设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
        [self addLegendHeaderWithRefreshingBlock:^{
            
            [weakSelf creatReaqstUrlDataView];
        }];
        


        [self creatReaqstUrlDataView];
        
    }
    
     return self;
}


-(void)creaMeDataZuxun
{
    //列表请求
    NSDictionary* dicParams = [NSDictionary dictionaryWithObjectsAndKeys:
                               [XSZ_UICommon useridChangUaerdfLoginBoolString],         @"user_id",
                               @"1",                                @"site_id",
                               [NSNumber numberWithInteger:1],      @"page_no",
                               [NSNumber numberWithInteger:20],     @"page_size",
                               @"articles",                         @"app",
                               @"article",                          @"model",
                               [NSNumber numberWithInteger:1],       @"consultant_id",
                               self.id_massge,                      @"object_id",
                               [NSNumber numberWithBool:YES],       @"for_me",
                               
                               nil];
    
    
    
    if (_httpRequest == nil)
    {
        _httpRequest = [[HXHttpRequest alloc] init];
        _httpRequest.delegate = self;
        
        
        
    }
    [_httpRequest requestForParams:dicParams andMethod:YZ_COUNSEL_CONSULTING_LIST andTag:1023];//@"system.time"];//
 
}




- (void)creatTitleHeadView
{
    UIView  *bgView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, Dev_ScreenWidth, 80)];
    bgView.backgroundColor =[UIColor clearColor];
    
    UIView  *titlebg =[[UIView alloc]initWithFrame:CGRectMake(0, 0, Dev_ScreenWidth, 40)];
    titlebg.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:titlebg];
    
    
    self.labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, Dev_ScreenWidth-20, 40)];
    self.labelTitle.font = [UIFont boldSystemFontOfSize:16];          //UILabel的字体大小
    self.labelTitle.numberOfLines = 1;                                   //必须定义这个属性，否则UILabel不会换行
    self.labelTitle.textColor = [UIColor grayColor];
    self.labelTitle.textAlignment = NSTextAlignmentLeft;                     //文本对齐方式
    //[labelTitle setBackgroundColor:[UIColor redColor]];
    [titlebg addSubview:self.labelTitle];
    
    
    UIView *viewredView = [[UIView alloc]initWithFrame:CGRectMake(10, 53, 2, 15)];
    viewredView.backgroundColor = RGBCOLOR(252, 116, 148);
    [titlebg addSubview:viewredView];
    
    
    UILabel *biaotiTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 40, Dev_ScreenWidth-20, 40)];
    biaotiTitle.font = [UIFont boldSystemFontOfSize:14];          //UILabel的字体大小
    biaotiTitle.numberOfLines = 1;                                   //必须定义这个属性，否则UILabel不会换行
    biaotiTitle.textColor = [UIColor grayColor];
    biaotiTitle.textAlignment = NSTextAlignmentLeft;                     //文本对齐方式
    //[biaotiTitle setBackgroundColor:[UIColor redColor]];
    biaotiTitle.text =@"相关回答" ;
    [bgView addSubview:biaotiTitle];
    
    self.tableHeaderView =bgView;

}

- (void)creatReaqstUrlDataView
{
    
    //列表请求
    NSDictionary* dicParams = [NSDictionary dictionaryWithObjectsAndKeys:
                               [XSZ_UICommon useridChangUaerdfLoginBoolString],         @"user_id",
                               @"1",                                @"site_id",
                               [NSNumber numberWithInteger:1],      @"page_no",
                               [NSNumber numberWithInteger:20],     @"page_size",
                               @"articles",                         @"app",
                               [NSNumber numberWithInteger:1],       @"consultant_id",
                               @"article",                          @"model",
                               self.id_massge,                     @"object_id",
                               nil];
    
    
    
    if (_httpRequest == nil)
    {
        _httpRequest = [[HXHttpRequest alloc] init];
        _httpRequest.delegate = self;
        
        
        
    }
    [_httpRequest requestForParams:dicParams andMethod:YZ_COUNSEL_CONSULTING_LIST andTag:1023];//@"system.time"];//


}


#pragma mark - HXHttpRequestDelegate  代理

- (void)requestFinished:(NSString *)responseString andStatusCode:(int)statusCode andTag:(NSInteger)tag
{
    
    
    // 停止刷新状态
    [self.legendHeader endRefreshing];
    
    NSDictionary *dic = [[NSDictionary alloc]init];
    dic = [JsonUtil jsonToDic:responseString];
    
    if ([dic[@"data"] isEqual:[NSNull null]])
    {
        return;
    }
    
    NSString * strs = dic[@"data"];
    
    NSDictionary *dics =[JsonUtil jsonToDic:strs];
    
    NSArray *arryy =(NSArray *)dics;
    
    NSLog(@"-----------> %@",arryy);
    
    
    
    [DataManage removeAllObjects];
    
    for (NSDictionary *dicuu in arryy)
    {
        AdvisoryModel  *modeContact =[[AdvisoryModel alloc]initWithDataManagement:dicuu];
       [DataManage addObject:modeContact];
    }
    
   
    [self reloadData];
    
    
}


#pragma mark  HXHttpRequest - 请求代理，，错误代理
- (void)requestFailed:(NSError *)error andTag:(NSInteger)tag
{
    
    
    
}




#pragma arguments - mark  UITableViewDelegate
// 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    return DataManage.count;
}


//设置每行高度（每行高度可以不一样）
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    return 150;
}



// IBOutlet UILabel *zixunkefu;
//
// IBOutlet UILabel *zixuntime;
//
// IBOutlet UILabel *zixunwenti;
//
// IBOutlet UILabel *zixunwentijuti;
//
//IBOutlet UILabel *zixunkefuhuida;


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static  NSString  *CellIdentiferId = @"AdvisoryCellID";
    AdvisoryForCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
    
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"AdvisoryForCell" owner:nil options:nil];
        cell = [nibs lastObject];
        // cell.backgroundColor = [UIColor clearColor];
        
        
    }
    
    if(DataManage.count<1)
    {
        return cell;
    }
    
    AdvisoryModel *cellmodel = [DataManage objectAtIndex:indexPath.row];
    
    cell.zixunkefu.text        = cellmodel.ReceiverName;
  if([cellmodel.ReplyContent isEqual:[NSNull null]])
  {
       cell.zixunkefuhuida.text   = @"    等待专家回复...";
  
  }else if (![cellmodel.ReplyContent isEqual:[NSNull null]])
  {
      cell.zixunkefuhuida.text   = [NSString stringWithFormat:@"    %@",cellmodel.ReplyContent];

  }
    
    
    cell.zixuntime.text        = cellmodel.SendTime;
    cell.zixunwenti.text       = cellmodel.Title;
    cell.zixunwentijuti.text   = [NSString stringWithFormat:@"    %@",cellmodel.Content];
  
    
    return cell;
}


//响应点击事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.row<DataManage.count)
    {
        AdvisoryModel   *modeldata = [DataManage objectAtIndex:indexPath.row];
        QuestDetailsVC  *readmassage =[[QuestDetailsVC alloc]init];
        readmassage.id_massge = modeldata.id_massge;
        [self.DelegatMessage.navigationController pushViewController:readmassage animated:YES];
    }
    
}



@end
